package com.mjk.rest.info;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.web.session.HttpServletSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.mjk.common.rest.BaseController;
import com.mjk.model.info.User;
import com.mjk.service.info.UserService;
import com.mjk.service.system.PermissionService;
import com.mjk.vo.IntactUser;
import com.mjk.vo.MenuTree;

import tk.mybatis.mapper.entity.Example;

@RestController
@RequestMapping("api/user")
public class UserController extends BaseController<UserService, User>{
	
	@Autowired
    private PermissionService permissionService;

//	@RequestMapping(value = "login", method = RequestMethod.POST)
//	public Map<String, Object> login(){
//		Map<String, Object> map = new HashMap<String, Object>();
//		Map<String, Object> userMap = new HashMap<String, Object>();
//		userMap.put("name", "黄章三");
//		userMap.put("sex", 1);
//		userMap.put("age", 23);
//		userMap.put("birth", 2017-01-01);
//		userMap.put("addr", "afdasdfasdf");
//		map.put("code", 200);
//		map.put("user", userMap);
//		return map;
//	}
	@RequestMapping(value = "login", method = RequestMethod.POST)
    public ResponseEntity<?> createAuthenticationToken(String username, String password) throws Exception {
        Example example = new Example(User.class);
        example.createCriteria().andEqualTo("username", username);
        example.createCriteria().andEqualTo("password", password);
        List<User> list = baseBiz.selectByExample(example);
        if(list == null || list.size() == 0) {
        	return ResponseEntity.status(200).body(false);
        }else{
        	return ResponseEntity.ok(list.get(0));
        }
    }
	
	@RequestMapping(value = "/intact/info", method = RequestMethod.GET)
	public ResponseEntity<?> getUserInfo(String id){
		if(StringUtils.isBlank(id)){
			return ResponseEntity.status(401).body(false);
		}else{
			IntactUser userInfo = permissionService.getUserInfo(id);
			if(userInfo == null){
				return ResponseEntity.status(401).body(false);
	        } else {
	            return ResponseEntity.ok(userInfo);
	        }
		}
	}
	
	@RequestMapping(value = "/intact/menus", method = RequestMethod.GET)
    public List<MenuTree> getMenusByUsername(String id) throws Exception {
		if(StringUtils.isBlank(id)){
			return null;
		}else{
			return permissionService.getMenusByUsername(id);
		}
    }
}
